{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "ce061973367468ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-10T09:28:07.778220600Z",
     "start_time": "2025-07-10T09:28:07.757061300Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.risk import IRAnnualImpliedVol, Price, IRFwdRate, IRAnnualATMImpliedVol, IRDelta\n",
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.common import MeasureScenario\n",
    "from gs_quant.markets import PricingContext\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8b13e419dcb97542",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-10T09:28:14.582241900Z",
     "start_time": "2025-07-10T09:28:08.144756700Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e9968f7e25e8115",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "MeasureScenario class allows us to override inputs such as spot or volatility when pricing an instrument. Here are some examples of how to use it with Interest rate swaption."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a647b948e0ccc30",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Initial IRSwaption\n",
    "swaption = IRSwaption(notional_currency='EUR', effective_date='1y', termination_date='10y')\n",
    "swaption.calc((IRAnnualImpliedVol, Price, IRFwdRate, IRAnnualATMImpliedVol)).to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "952616a7c77ff7c2",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Override a single input with MeasureScenario context manager\n",
    "with MeasureScenario(IRFwdRate, 0.024):\n",
    "    risk = swaption.calc((IRAnnualImpliedVol, Price, IRFwdRate, IRAnnualATMImpliedVol))\n",
    "risk.to_frame()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bec831f54e74bd45",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can see that the Interest rate forward rate value was overridden, and other variables are re-calculated to account for the new rate."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e566074c80751e50",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We might also be interested in comparing the forward rate between the original, up, and down override scenarios."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72d934f4c7eaa031",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "init = swaption.calc((IRDelta(aggregation_level='Type'), Price, IRFwdRate))\n",
    "init.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b9f77b4037c3b693",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-10T09:28:32.540887600Z",
     "start_time": "2025-07-10T09:28:29.404805700Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with MeasureScenario(IRFwdRate, init[IRFwdRate] + 0.0001):\n",
    "    up = swaption.calc((Price, IRFwdRate))\n",
    "\n",
    "with MeasureScenario(IRFwdRate, init[IRFwdRate] - 0.0001):\n",
    "    down = swaption.calc((Price, IRFwdRate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5839afc1037abd1",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(init[IRFwdRate])\n",
    "print(up[IRFwdRate])\n",
    "print(down[IRFwdRate])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d24dcf9e6bef345f",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "The forward rates are close in 3 scenarios, which is expected as the swaption expiry date is in 1 year only. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1e9c04ed2bb53be",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can also override multiple inputs.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e7cec45c7b0756a",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Override multiple inputs (with nested context managers)\n",
    "with MeasureScenario(IRFwdRate, 0.028):\n",
    "    with MeasureScenario(IRAnnualImpliedVol, 0.009):\n",
    "        risk = swaption.calc((IRAnnualImpliedVol, Price, IRFwdRate, IRAnnualATMImpliedVol))\n",
    "risk.to_frame()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52670824ba328019",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can also combine the measure override with other context managers, such as PricingContext\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a36367b5758d79fd",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Combine with PricingContext context manager\n",
    "with PricingContext(dt.date(2025, 7, 5)):\n",
    "    with MeasureScenario(IRAnnualImpliedVol, 0.002):\n",
    "        risk = swaption.calc((IRAnnualImpliedVol, Price, IRFwdRate, IRAnnualATMImpliedVol))\n",
    "risk.result().to_frame()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
